package com.zhkj.txg.module.home.ui

import android.app.Activity
import android.content.Intent
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import com.chad.library.adapter.base.entity.node.BaseNode
import com.zhkj.lib.base.ViewModelActivity
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.lib.utils.AccountManager
import com.zhkj.txg.R
import com.zhkj.txg.module.home.adapter.ShopCateAdapter
import com.zhkj.txg.module.home.entity.ShopEntity
import com.zhkj.txg.module.home.vm.ShopViewModel
import com.zhkj.txg.utils.ImageUtils
import kotlinx.android.synthetic.main.activity_shop_category.*

@Suppress("UNCHECKED_CAST")
class ShopCategoryActivity : ViewModelActivity<ShopViewModel>() {
    companion object {
        private val SHOP = "shop"

        fun open(activity: Activity, shop: ShopEntity, requestCode: Int) {
            val intent = Intent(activity, ShopCategoryActivity::class.java).putExtra(SHOP, shop)
            activity.startActivityForResult(intent, requestCode)
        }
    }

    override fun setViewModel(): ShopViewModel = createViewModel()
    override fun statusTitleBar(): View? = titleShopCate
    override val layoutResId: Int = R.layout.activity_shop_category

    private val cateAdapter: ShopCateAdapter by lazy {
        ShopCateAdapter(viewModel.shop?.shopName ?: "", viewModel.shop?.id?.toLong() ?: 0)
    }
    private var refresh = false

    override fun initViewModel() {
        viewModel.shop = intent.getSerializableExtra(SHOP) as ShopEntity
        viewModel.followLiveData.observe(this, Observer { response ->
            handlerResponseStatus(response.status, response.statusTip)
            if (response.status == HttpResponseStatus.Status.SUCCESS) response.response?.let {
                // 数据刷新
                tvShopCollect.isSelected = it.isFollow
                tvShopCollect.text =
                    getString(if (it.isFollow) R.string.followed else R.string.not_follow)
                tvShopFansCount.text = String.format(getString(R.string.fans_count), it.followCount)
                toast(getString(if (it.isFollow) R.string.followed else R.string.followed_cancel))
                // 判断是否需要刷新
                refresh = true
            }
        })
    }

    override fun initUI() {
        with(rvShopCate) {
            layoutManager = GridLayoutManager(this@ShopCategoryActivity, 2)
            adapter = cateAdapter
        }
        with(cateAdapter) {
            setEmptyView(R.layout.layout_empty_data)
        }
        // 关注店铺
        tvShopCollect.setOnClickListener {
            viewModel.shop?.let {
                viewModel.followShop(
                    AccountManager.getToken(),
                    it.id,
                    tvShopCollect.isSelected
                )
            }
        }
        // 打开搜索页面
        tvShopTopSearch.setOnClickListener {
            viewModel.shop?.let {
                SearchActivity.shopOpen(this, it.id.toLong())
            }
        }
    }

    override fun initData() {
        viewModel.shop?.let {
            tvShopName.text = it.shopName
            tvShopGoodsCount.text = String.format(getString(R.string.sale_count), it.goodsCount)
            tvShopFansCount.text = String.format(getString(R.string.fans_count), it.collectCount)
            tvShopCollect.isSelected = it.isCollect
            tvShopCollect.text =
                getString(if (it.isCollect) R.string.followed else R.string.not_follow)
            ImageUtils.loadCircleImage(this@ShopCategoryActivity, ivShopTop, it.shopImg)
        }
        viewModel.shopCate(viewModel.shop?.id ?: 0).takeIf {
            !it.hasObservers()
        }?.observe(this, Observer { response ->
            handlerResponseStatus(response.status, response.statusTip)
            if (response.status == HttpResponseStatus.Status.SUCCESS) response.response?.let {
                cateAdapter.setNewData(it.data as? MutableList<BaseNode>)
            }
        })
    }

    override fun finish() {
        intent.putExtra("data", viewModel.shop)
        setResult(RESULT_OK, intent)
        super.finish()
    }
}